// src/components/LoginForm.jsx
import { Form, Input, Button, message } from 'antd';
import { login } from '../services/auth';
import { useNavigate } from 'react-router-dom';
export default function LoginForm() {
  const navigate = useNavigate();

  const onFinish = async (values) => {
    try {
      const res = await login(values.email, values.password);
      message.success('登录成功');
      console.log('登陆成功',res);
      localStorage.setItem('token', res.data.token);
      navigate('/home');
    } catch (err) {
      message.error('登录失败，请检查邮箱或密码');
    }
  };

  return (
    <Form name="login" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }} onFinish={onFinish}>
      <Form.Item
        label="邮箱"
        name="email"
        rules={[
          { required: true, message: '请输入邮箱' },
          { type: 'email', message: '请输入有效的邮箱地址' },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="密码"
        name="password"
        rules={[{ required: true, message: '请输入密码' }]}
      >
        <Input.Password />
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 6, span: 16 }}>
        <Button type="primary" htmlType="submit" block>
          登录
        </Button>
        <div style={{ marginTop: '10px', textAlign: 'right' }}>
          <a href="/register">没有账号？去注册</a>
        </div>
      </Form.Item>
    </Form>
  );
}
